package com.example.myserializeprojects;

import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ContentHandler extends DefaultHandler {
    private final String TAG = "ContentHandler";
    public String nodeName;
    public StringBuilder id;
    public StringBuilder name;
    public StringBuilder version;

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        Log.e(TAG, "startDocument: " );

        id = new StringBuilder();
        name = new StringBuilder();
        version = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        nodeName = localName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if ("id".equals(nodeName)){
            id.append(ch, start, length);
        } else if ("name".equals(nodeName)){
            name.append(ch, start, length);
        } else if ("version".equals(nodeName)){
            version.append(ch, start, length);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("apps".equals(localName)){
            Log.e(TAG, "endElement: id "+id.toString().trim()+" name " +name.toString().trim()+" version "+version.toString().trim());
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
        super.endElement( uri,  localName,  qName);
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        Log.e(TAG, "endDocument: " );
    }
}
